
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class GenericTypeExample {

    public List<String> getItems() {
        List<String> objects = new ArrayList<>();
        objects.add("ac");
        return objects;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Method method = GenericTypeExample.class.getMethod("getItems");
        MethodParameter methodParameter = new MethodParameter(method, -1); // -1 表示返回类型

        // 使用 getParameterType()
        Class<?> parameterType = methodParameter.getParameterType();
        System.out.println("getParameterType(): " + parameterType.getName()); // 输出: java.util.List

        // 使用 ResolvableType
        ResolvableType resolvableType = ResolvableType.forMethodParameter(methodParameter);
        Class<?> resolvedType = resolvableType.resolve();
        System.out.println("ResolvableType.resolve(): " + resolvedType.getName()); // 输出: java.util.List

        // 检查泛型参数
        if (resolvableType.hasGenerics()) {
            ResolvableType[] genericTypes = resolvableType.getGenerics();
            for (ResolvableType genericType : genericTypes) {
                System.out.println("Generic Type: " + genericType.getType().getTypeName()); // 输出: java.lang.String
            }
        }
    }
}
